/*MyPhotoIndex Free Photo Organizer <www.myphotoindex.com>
  Copyright (C) <2006-2009>  <Asaf Yarkoni asaf@yarkoni.net>

  This program is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.*/
using System;
using System.Collections.Generic;
using System.Windows.Forms;
using MyPhotoIndex.DataAccess;
using MyPhotoIndex.ImageBank;
using System.Threading;
using MyPhotoIndex.Properties;
using System.Globalization;
using System.IO;
using MyPhotoIndex.Plugins;
using MyPhotoIndex.Utilities;
using MyPhotoIndex.Settings;
using MyPhotoIndex.Plugins.ImageFormatProviders;
using MyPhotoIndex.MainMDI;
using System.Diagnostics;
using System.Reflection;

namespace MyPhotoIndex
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {  
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (HandleAppInstance() == false)
            {
                Logger.Log("Application is allready running");
                return;
            }

            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);

            Manager.Init(ApplicationFolders.PluginsFolder);

            String currentUICulture = ApplicationSettings.Default.DefaultLanguage;
            CultureInfo uiCulture = new CultureInfo(currentUICulture);
            Thread.CurrentThread.CurrentUICulture = uiCulture;

            try
            {
                Thread.CurrentThread.CurrentUICulture.DateTimeFormat.ShortDatePattern = ApplicationSettings.Default.DisplayDateFormat;
            }
            catch (Exception ex)
            {
                Logger.Log(ex);
            }

            SplashScreen.Instance.StartPosition = FormStartPosition.CenterScreen;
            SplashScreen.Instance.TopMost = true;
            SplashScreen.Instance.Show();

            while (SplashScreen.Instance.Opacity < 1)
            {
                Application.DoEvents();
            }

            Application.Run(new Main());

            Manager.UnloadPlugins();
            Storage.Instance.Dispose();
            DataAccessProvider.Instance.Dispose();
        }

        static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            Logger.Log(e.Exception);
        }

        private static Mutex ms_appSingleton;
        private static bool HandleAppInstance()
        {
            bool instantiated = false;
            ms_appSingleton = new Mutex(false, "Local\\" + "{614A3D0F-D10A-4c0b-96AC-DBD98CD21975}", out instantiated);

            if (instantiated == true)
            {
                return true;
            }

            Process[] processes = Process.GetProcessesByName("MyPhotoIndex");
            foreach (Process process in processes)
            {

                IntPtr pFoundWindow = process.MainWindowHandle;
                if (pFoundWindow != IntPtr.Zero)
                {
                    Win32Interop.ShowWindow(pFoundWindow, Win32Interop.SW_SHOWMAXIMIZED);
                    Win32Interop.SetForegroundWindow(pFoundWindow);
                }
            }

            return false;
        }
    }
}